﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rabbit.Model;
using System.Diagnostics;

namespace Rabbit.BusinessLogicLayer
{
    public class FriendStatusBL:AutoUpdateMessageClassBase<Status>
    {
        public FriendStatusBL(double UpdateInterval):base(UpdateInterval)
        {

        }

        public List<Status> GetMoreFriendsStatus()
        {
            List<Status> statusList = StatusesBL.GetMoreFriendsStatuses();
            StatusesBL.GetForwardedStatusPicture(statusList);
            StatusesBL.GetStatusCommentAndTransmitCount(statusList);
            return statusList;
        }

        protected override void FetchNewMessage()
        {  
            Debug.Print("fetch friend status: Last Update Time:" + LastUpdateTime);
            List<Status> newStatusList = StatusesBL.GetFriendsStatuses(LastMaxID);
            Debug.Print("\t\t\t\t new status count:" + newStatusList.Count);
            newStatusList.ForEach(s => s.IsNew = true);
            if (newStatusList.Count > 0)
            {
#if DEBUG
                newStatusList.ForEach(s => {
                    Debug.Print("status id:" + s.Id + " create at:" + s.CreatedAt + " text:" + s.Text);
                });
#endif
                base.LastMaxID = newStatusList.Max(s => s.Id);
            }

            //get the forwarded status picture url
            StatusesBL.GetForwardedStatusPicture(newStatusList);

            //batch get statuses' transmit and comment count, this includes:
            //MainMessageList which is passed from the UI, and the new status list
            List<Status> tempStatusList = new List<Status>();
            tempStatusList.AddRange(newStatusList);
            if (this.MainMessageList != null)
            {
                MainMessageList.ToList().ForEach(s => tempStatusList.Add(s));
            }
            StatusesBL.GetStatusCommentAndTransmitCount(tempStatusList);

            RaiseNewMessageReceivedEvent(newStatusList);
        }

        protected override void FetchMessage()
        {
            List<Status> statusList = StatusesBL.GetFriendsStatuses();
            base.LastMaxID = statusList.Max(s => s.Id);

            StatusesBL.GetForwardedStatusPicture(statusList);
            StatusesBL.GetStatusCommentAndTransmitCount(statusList);
            RaiseMessageFetchedEvent(statusList);
        }

        public override List<Status> GetNextPageMessage()
        {
            throw new NotImplementedException();
        }

        public override List<Status> GetPreviousePageMessage()
        {
            throw new NotImplementedException();
        }

        public override List<Status> GetFirstPageMessage()
        {
            throw new NotImplementedException();
        }
    }
}
